//
// C++ Implementation: xmlconfig

//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "xmlconfig.h"
#include <QtDebug>

/**
*   Constructor.
*   TODO: need to set config directory somewhere...
*/
XMLConfig::XMLConfig()
{
    iodevice = new QFile ( "config.xml" );

    if ( !iodevice->open ( QIODevice::ReadOnly ) )
    {
        // TODO: handle errors correctly
        qFatal("Failed to open xml config file.");
    }
    // given an existing file, it should be opened correctly here

}

/**
*   Constructor.
*   @param iodevice The xml config file to be read.
*   TODO: need to set config directory somewhere...
*/
XMLConfig::XMLConfig ( QIODevice* iodevice )
{
    this->iodevice = iodevice;

    if ( !iodevice->open ( QIODevice::ReadOnly ) )
    {
        // TODO: handle errors correctly
        qFatal("Failed to open xml config file.");
    }
}


XMLConfig::~XMLConfig()
{
}


/**
*   Mutator to set the file/device from which to read the xml config file.
*   @param iodevice The xml config to be read.
*   @return True on successful set and open.
*/
bool XMLConfig::setIODevice(QIODevice* iodevice)
{
    this->iodevice = iodevice;

    if ( !iodevice->open ( QIODevice::ReadOnly ) )
    {
        // TODO: handle errors correctly
        qFatal("Failed to open xml config file.");
        return false;
    }
    return true;
}


/**
*   Creates the Document Object Model from the given file.
*   @return True on successful creation.
*/
bool XMLConfig::createDOM()
{
    dom = new QDomDocument ( "XmlConfigFile" );

    if ( !dom->setContent ( iodevice ) )
    {
        // TODO: handle errors correctly
        qWarning("Failed setting content of DOM model.");
        return false;
    }


    iodevice->close();
    return true;
}

/**
*   Mutator to set the DOM object of the xml config file.
*   @param dom The DOM object to set.
*   @return True on successful set.
*/
bool XMLConfig::setDOM(QDomDocument* dom)
{
    this->dom = dom;
    return true;
}


/**
 * A method to match the current configuration version with
 * the program's version.
 * @param version The current version of the program.
 * @return False on no match for current version, true for match.
 */
bool XMLConfig::matchConfigVersion(QString version)
{
    QDomElement element = dom->documentElement();
    element = element.firstChildElement("configuration");
    while(!element.isNull())
    {
	if(element.hasAttribute("version"))
	{
	    if(element.attribute("version") == version)
	    {
		currentConfigElement = element.cloneNode().toElement();
		return true;
	    }
	}
	element = element.nextSiblingElement("configuration");
    }

    return false;
}

/**
 * Does the extraction of the configuration settings
 * from an XML document.
 * @return true on successful extraction.
 */
bool XMLConfig::extractConfig()
{
    QDomElement element = currentConfigElement;
    QDomElement sqlElement;

    sqlElement = element.firstChildElement("sql");
    if(!sqlElement.isNull())
    {
	element = sqlElement.firstChildElement("sql:type");
	if(!element.isNull())
	{
	    type = element.text().toUpper();
	}
	else
	{
            qWarning("Failed to determine database type");
	    return false;
	}

	element = sqlElement.firstChildElement("sql:host");
	if(!element.isNull())
	{
	    host = element.text();
	}
	else
	{
	    qWarning("Failed to determine database host");
	    return false;
	}

	element = sqlElement.firstChildElement("sql:port");
	if(!element.isNull())
	{
	    port = element.text().toInt();
	}
	else
	{
	    qWarning("Failed to determine database port");
	    return false;
	}

	element = sqlElement.firstChildElement("sql:database");
	if(!element.isNull())
	{
	    database = element.text();
	}
	else
	{
	    qWarning("Failed to determine database name");
	    return false;
	}

	element = sqlElement.firstChildElement("sql:username");
	if(!element.isNull())
	{
	    username = element.text();
	}
	else
	{
	    qWarning("Failed to determine database username");
	    return false;
	}

	element = sqlElement.firstChildElement("sql:password");
	if(!element.isNull())
	{
	    password = element.text();
	}
	else
	{
	    qWarning("Failed to determine database password");
	    return false;
	}
    }
    else
    {
        qWarning("Failed to find database section of config file");
	return false;
    }
    return true;
}



